package com.mrlv.rabbitmq;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author Mrlv
 * @version 1.0
 * @description: 信号量
 * @date 2021年11月19日 12:10
 */
public class SemaphoreDemo {


    public static void main(String[] args) {

        Semaphore semaphore = new Semaphore(3);

        for (int i = 0; i < 6; i++) {
            new Thread(() ->{
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + "占据了车位");

                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(Thread.currentThread().getName() + "离开了车位");
                    semaphore.release();

                }
            }, String.valueOf(i)).start();
        }
    }
}
